/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testis.gui.mvc.controller.testbed;

import org.testis.gui.mvc.controller.AbstractController;
import org.testis.gui.mvc.controller.TestBedControllerList;
import org.testis.gui.mvc.model.testbed.NetworkAdapterModel;
import org.testis.gui.mvc.view.testbed.NetworkAdapterView;
import org.testis.orm.testbed.refined.NetworkAdapterRefined;
import org.testis.orm.testbed.refined.NetworkAdapterTypeRefined;
import org.testis.orm.testbed.refined.TestBedItem;
import org.testis.orm.testbed.refined.TestPcRefined;
import org.testis.orm.testbed.datahandlers.NetworkAdapterDataHandler;
import org.testis.orm.testbed.refined.NetworkAdapterFactory;
import org.testis.utils.tree.TbedMutableTreeNode;
import java.util.HashMap;

/**
 *
 * @author selcuk
 */
public class NetworkAdapterController extends AbstractController {

    public static final String VISIBLE_PROPERTY = "Visibility";
    public static final String ERROR_PROPERTY = "LastError";
    public static final String ADAPTER_REFINED_PROPERTY = "AdapterRefined";
    public static final String ADAPTER_ID_PROPERTY = "AdapterId";
    public static final String ADAPTER_TYPE_ID_PROPERTY = "AdapterTypeId";
    public static final String NAME_PROPERTY = "Name";
    public static final String TEST_PC_ID_PROPERTY = "TestPcId";
    private TbedMutableTreeNode m_selectedNode = null;

    public NetworkAdapterController() {
        super();
    }

    private void initialize(boolean editMode) {
        NetworkAdapterModel model = new NetworkAdapterModel();
        NetworkAdapterView view = new NetworkAdapterView(
                TestBedControllerList.newInstance().getParentFrame(),
                true, editMode);
        super.addModel(model);
        super.addView(view);
        view.pack();
    }

    public void operationCloseWindow() {
        super.cleanAll();
    }

    public void operationEditAdapter(TbedMutableTreeNode node) {
        if (node.getUserObject() instanceof NetworkAdapterRefined) {
            m_selectedNode = node;
            NetworkAdapterRefined adapterRefined = getSelectedAdapter();
            if (adapterRefined == null) {
                System.out.println(
                        "Not found design part for NetworkAdapterRefined");
                return;
            }
            initialize(true);

            setModelProperty(ADAPTER_REFINED_PROPERTY, adapterRefined);
            setModelProperty(ADAPTER_ID_PROPERTY, adapterRefined.getId());
            setModelProperty(NAME_PROPERTY, adapterRefined.getName());

            TestPcRefined testPc = adapterRefined.getPc();
            if (testPc != null) {
                setModelProperty(TEST_PC_ID_PROPERTY, testPc.getId());
            }

            NetworkAdapterTypeRefined adapterType = adapterRefined.getAdapterType();
            if (adapterType != null) {
                setModelProperty(ADAPTER_TYPE_ID_PROPERTY, adapterType.getId());
            }

            setModelProperty(VISIBLE_PROPERTY, true);
        }
    }

    public void operationNewAdapter() {
        initialize(false);
        TestBedItem _testBedItem = TestBedControllerList.newInstance().getTbedTreeController().getSelectedTestBedItem();
        if (_testBedItem != null && _testBedItem instanceof TestPcRefined) {
            TestPcRefined _selectedTestPC = (TestPcRefined) _testBedItem;
            setModelProperty(TEST_PC_ID_PROPERTY, _selectedTestPC.getId());
        }
        setModelProperty(VISIBLE_PROPERTY, true);
    }

    private NetworkAdapterRefined getSelectedAdapter() {
        NetworkAdapterRefined result = null;
        Object obj = m_selectedNode.getUserObject();
        if (obj != null && obj instanceof NetworkAdapterRefined) {
            result = (NetworkAdapterRefined) obj;
        }
        return result;
    }

    public void operationDeleteAdapter(NetworkAdapterRefined refined) {
        if (NetworkAdapterFactory.newInstance().deleteAdapter(refined)) {
            TestBedControllerList.newInstance().getTbedTreeController().operationRemoveSelectedTestBedItem();
        } else {
            TestBedControllerList.newInstance().getTbedTreeController().operationSetErrorProperty(
                    "Unable to remove NetworkAdapter record");
        }
    }

    public void operationDeleteSelectedAdapter() {
        TestBedItem tbedItem = TestBedControllerList.newInstance().getTbedTreeController().getSelectedTestBedItem();
        if (tbedItem != null && tbedItem instanceof NetworkAdapterRefined) {
            NetworkAdapterRefined refined = (NetworkAdapterRefined) tbedItem;
            operationDeleteAdapter(refined);
        }
    }

    public void operationSaveAdapter(NetworkAdapterRefined refined, HashMap values) {
        Integer id = (Integer) values.get("adapterId");
        String name = (String) values.get("name");
        Integer typeId = (Integer) values.get("typeId");
        Integer pcId = (Integer) values.get("pcId");
        String wpaDriver = (String) values.get("wpaDriver");

        boolean move = (refined.getPc().getId() != pcId);
        if (NetworkAdapterDataHandler.newInstance().saveAdapter(id,
                name, typeId, pcId, wpaDriver)) {
            if (move) {  //move node from one to another              
                TestBedControllerList.newInstance().getTbedTreeController().operationMoveSelectedTestBedItem(
                        pcId);
            }
        } else {
            setModelProperty(ERROR_PROPERTY,
                    "Unable to edit network adapter record");
        }
    }

    public void operationAddAdapter(String name, int typeId, int pcId, String wpaDriver) {
        NetworkAdapterRefined refined = NetworkAdapterFactory.newInstance().createAdapter(
                name, typeId, pcId, wpaDriver);
        if (refined != null) {
            TestBedControllerList.newInstance().getTbedTreeController().operationAddTestBedItem(
                    refined);
        } else {
            setModelProperty(ERROR_PROPERTY,
                    "Unable to add NetworkAdapter record");
        }
    }
}
